Page({
  data: {
    activeTab: 'added', // 'added' 或 'draft'
    addedProducts: [],
    draftProducts: []
  },

  onLoad: function() {
    this.getProductList();
  },

  onShow: function() {
    // 页面显示时刷新数据
    this.getProductList();
  },

  switchTab: function(e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({
      activeTab: tab
    });
  },

  getProductList: function() {
    // 从服务器获取商品列表
    // 模拟数据
    const addedProducts = [
      {
        id: 1,
        name: '烤面筋',
        image: '/images/tabbar/product.png',
        price: 15.00,
        sales: 200
      },
      {
        id: 2,
        name: '烤肉串',
        image: '/images/tabbar/business.png',
        price: 25.00,
        sales: 150
      }
    ];

    const draftProducts = [
      {
        id: 101,
        name: '烤鱿鱼',
        image: '/images/tabbar/profile.png',
        price: 30.00,
        status: '未完成'
      }
    ];

    this.setData({
      addedProducts,
      draftProducts
    });
  },

  addProduct: function() {
    wx.navigateTo({
      url: '/pages/products/add/add'
    });
  },

  editProduct: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/products/edit/edit?id=${id}`
    });
  },

  deleteProduct: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确认删除',
      content: '确定要删除这个商品吗？',
      success: (res) => {
        if (res.confirm) {
          // 调用删除商品API
          console.log('删除商品，ID:', id);
          // 刷新列表
          this.getProductList();
        }
      }
    });
  },

  publishProduct: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确认发布',
      content: '确定要发布这个商品吗？',
      success: (res) => {
        if (res.confirm) {
          // 调用发布商品API
          console.log('发布商品，ID:', id);
          // 刷新列表
          this.getProductList();
        }
      }
    });
  }
}); 